<template>
  <div class="home">
    <div class="zengjia">
      <h2>列表详情</h2>
      <a-button
        type="primary"
        @click="
          () => {
            this.$router.push('/app/artAdd');
          }
        "
      >
        增加
      </a-button>
    </div>
    <a-table
      :columns="columns"
      :rowKey="(item) => item.id"
      :data-source="lists"
      :pagination="{
        pageSize: this.pageSize,
        total: this.total,
      }"
      @change="changSize"
    >
    </a-table>
  </div>
</template>
<script>
import { listItems,delArt } from "@api";
var dayjs = require("dayjs");
export default {
  data() {
    const columns = [
      {
        title: "作者",
        dataIndex: "author",
        key: "author",
      },
      {
        title: "文章标题",
        dataIndex: "title",
        key: "title",
      },
      {
        title: "文章描述",
        dataIndex: "desc",
        key: "desc",
        customRender: (value) => {
          return (
            <a-tooltip>
              <template slot="title">{value}</template>
              {value.substr(0, 10) + "..."}
            </a-tooltip>
          );
        },
      },
      {
        title: "文章封面",
        dataIndex: "thumb",
        key: "thumb",
        customRender: (value) => {
          return <img src={value}></img>;
        },
      },
      {
        title: "点赞次数",
        dataIndex: "zan",
        key: "zan",
        customRender: (value) => {
          return <a-tag color={value > 400 ? "red" : "cyan"}>{value}</a-tag>;
        },
      },
      {
        title: "创建时间",
        dataIndex: "createAt",
        key: "createAt",
        customRender: (value) => {
          return dayjs(value).format("YYYY" + "年" + "MM" + "月" + "DD" + "日");
        },
      },
      {
        title: "操作",
        customRender: (row) => {
          return (
            <div>
              <a-button
                onClick={() => {
                  this.$router.push(`/app/artEdit/${row.id}`);
              
                }}
                type="primary"
              >
                编辑
              </a-button>
              <a-button danger onClick={()=>{
               this.delArt(row.id)
              }}>删除</a-button>
            </div>
          );
        },
      },
    ];
    return {
      columns,
      page: 1,
      pageSize: 10,
      lists: [],
      total: 0,
    };
  },
  created() {
    this.getListItems();
  },
  methods: {
    changSize(Tabs) {
      this.page = Tabs.current;
      this.pageSize = Tabs.pageSize;
      this.getListItems();
    },
    delArt(id){
      delArt(id).then(res=>{
        console.log(res);
        if(res.data.code===200){
   
        }
      })
    },
    getListItems() {
      listItems({
        page: this.page,
        pageSize: this.pageSize,
      }).then((res) => {
        if (res.data.code === 200) {
          this.lists = res.data.data.lists;
          this.total = res.data.data.total;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.zengjia {
  display: flex;
  justify-content: space-between;
  padding: 10px 20px;
}
</style>